import 'package:WebTest/chooseFood/core/model/category_model.dart';
import 'package:WebTest/chooseFood/core/services/json_parse.dart';
import 'package:WebTest/chooseFood/ui/pages/home/home_category_item.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../../../../screenLayout/size_fit.dart';

//class BDHomeContent extends StatefulWidget {
//  @override
//  _BDHomeContentState createState() => _BDHomeContentState();
//}
//
//class _BDHomeContentState extends State<BDHomeContent> {
//  List<BDCategoryModel> _categories = [];
//
//  @override
//  void initState() {
//    // TODO: implement initState
//    super.initState();
////    加载数据
//    JsonParse.getCategoryData().then((res) {
//      setState(() {
//        _categories = res;
//      });
//    });
//  }
//
//  @override
//  Widget build(BuildContext context) {
//    return GridView.builder(
//      padding: EdgeInsets.all(20.px),
//      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
//        crossAxisCount: 2,
//        crossAxisSpacing: 20.px,
//        mainAxisSpacing: 20.px,
//        childAspectRatio: 1.5
//      ),
//      itemCount: _categories.length,
//      itemBuilder: (BuildContext context, int index) {
//        return BDHomeCategoryItem(_categories[index]);
//      },
//    );
//  }
//}

class BDHomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<BDCategoryModel>>(
      future: JsonParse.getCategoryData(),
      builder: (context,snapshot){
        if(!snapshot.hasData){
          return Center(child: CircularProgressIndicator());
        }
        else{
          final categories = snapshot.data;
          return GridView.builder(
            padding: EdgeInsets.all(20.px),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                crossAxisSpacing: 20.px,
                mainAxisSpacing: 20.px,
                childAspectRatio: 1.5),
            itemCount: categories.length,
            itemBuilder: (BuildContext context, int index) {
              return BDHomeCategoryItem(categories[index]);
            },
          );
        }
      },
    );
  }
}

